package controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import pojo.Good;
import pojo.JsonResult;
import data.MemoryData;

@Controller
@RequestMapping("/good")
public class GoodController extends BaseController{
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult<String> add(@RequestBody Good good) {
		JsonResult<String> rs = new JsonResult<String>();
		if (null == good || good.goodName.isEmpty() || good.count == 0){
			rs.statusCode = 1;
			rs.message = "Good infor error!!";
		} else {
			String userName = this.getUserName();
			if (MemoryData.goods.containsKey(userName)) {
				MemoryData.goods.get(userName).add(good);
			} else {
				List<Good> list = new ArrayList<Good>();
				list.add(good);
				MemoryData.goods.put(userName, list);
			}
			rs.statusCode = 0;
			rs.message = "Add good Success!!";
		}
		return rs;
	}
	
	@RequestMapping(value = "/list")
	@ResponseBody
	public JsonResult<List<Good>> list() {
		String userName = this.getUserName();
		return new JsonResult<List<Good>>(MemoryData.goods.get(userName));
	}
	
	@RequestMapping(value = "/listAll")
	@ResponseBody
	public JsonResult<Map<String, List<Good>>> listAll() {
		return new JsonResult<Map<String, List<Good>>>(MemoryData.goods);
	}
}
